using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class ComplaintDAL
    {
        public static int Save(ComplaintInfo info)
        {
            string sql = string.Format("insert into [complaint]([ArticleID],[UserID],[Complaints],[Fcontent],[Screenshot],[CreateDate]) values(@ArticleID,@UserID,@Complaints,@Fcontent,@Screenshot,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ArticleID", info.ArticleID),
                new SqlParameter("@UserID", info.UserID),
                new SqlParameter("@Complaints", info.Complaints),
                new SqlParameter("@Fcontent", info.Fcontent),
                new SqlParameter("@Screenshot", info.Screenshot),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar2(sql, paras));
        }
        public static int Update(ComplaintInfo info)
        {
            string sql = string.Format("update [complaint] set [ArticleID]=@ArticleID,[Complaints]=@Complaints,[Fcontent]=@Fcontent,[Screenshot]=@Screenshot where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ArticleID", info.ArticleID),
                new SqlParameter("@Complaints", info.Complaints),
                new SqlParameter("@Fcontent", info.Fcontent),
                new SqlParameter("@Screenshot", info.Screenshot),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static ComplaintInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[complaint]");
            return Model(dr);
        }
        public static List<ComplaintInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[complaint]");
            return ListModel(dt);
        }
        public static List<ComplaintInfo> FindPage(string keyword,int startindex, int pagesize, string order, string desc, out int count)
        {
            string filter ="";
            if (!string.IsNullOrEmpty(keyword))
            {
                filter+=" and [complaint].Name like '%"+keyword+"%'";
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[complaint]", out count);
            return ListModel(dt);
        }
        public static List<ComplaintInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [complaint]"));
            return ListModel(dt);
        }
        private static ComplaintInfo Model(DataRow dr)
        {
            ComplaintInfo info = new ComplaintInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.ArticleID = DataConvert.ToInt(dr["ArticleID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.Complaints = DataConvert.ToString(dr["Complaints"]);
                info.Fcontent = DataConvert.ToString(dr["Fcontent"]);
                info.Screenshot = DataConvert.ToString(dr["Screenshot"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static ComplaintInfo Model(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                return Model(dt.Rows[0]);
            }
            else
            {
                return new ComplaintInfo();
            }
        }
        private static List<ComplaintInfo> ListModel(DataTable dt)
        {
            List<ComplaintInfo> list = new List<ComplaintInfo>();
            DataColumnCollection col = dt.Columns;
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Model(row));
            }
            return list;
        }
    }
}

